package com.itheima.common.util;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @Author：shenkunlin
 * @date： 2021/8/30 16:40
 * @Description：
 ***/
@Configuration
public class OpenFeignIntercetor implements RequestInterceptor {

    /**
     * 拦截方法
     * @param requestTemplate
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //1:获取当前请求的所有请求头数据
        // token:dsfdgdfgdf.dfgdfgfdg.dfgfdgdfgdh
        HttpServletRequest request = RequestContext.getRequest();
        if(request!=null){
            Enumeration<String> headerNames = request.getHeaderNames();
            if(headerNames!=null){
                while (headerNames.hasMoreElements()){
                    String key = headerNames.nextElement();
                    String value = request.getHeader(key);
                    //2:执行远程请求的对象中封装请求头信息
                    requestTemplate.header(key,value);
                }
            }
        }
    }
}
